package cn.manter.system.controller;

import cn.manter.basic.utils.AjaxResult;
import cn.manter.basic.utils.PageList;
import cn.manter.system.Service.IDetailService;
import cn.manter.system.domain.Detail;
import cn.manter.system.query.DetailQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;
    @GetMapping("/{id}")
    public Detail loadById(@PathVariable("id") Long id){
        return detailService.loadById(id);
    }

    @GetMapping
    public List<Detail> loadAll(){
        return detailService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        System.out.println("明细删除方法");
        try {
            detailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.save(detail);
            }else {
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping
    public PageList<Detail> queryData(@RequestBody DetailQuery detailQuery){

        PageList<Detail> data = detailService.queryData(detailQuery);
        return data;
    }

    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            detailService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    @GetMapping("/dictionary/{id}")
    public List<Detail> loadByDetailId(@PathVariable("id") Long id){
        System.out.println("2222");
        return detailService.loadByDetailId(id);
    }

}
